layui.use(['table', 'admin', 'form', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    var mdzxTable = {
        tableId: "mdzxTable"
    };

    var queryData = {};
    queryData['btyZxMdzxId'] = Feng.getUrlParam("btyZxMdzxId");

    table.render({
        elem: '#mdzxTable',
        url: Feng.ctxPath + '/mdzx/tzqkInfo',
        where: queryData,
        cellMinWidth: 130,//全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols: [[
            {field: 'firstLevelName', width: 100, title: '一级科目', align: "center"},
            {field: 'firstLevelId', width: 100, hide: true, title: '一级科目id', align: "center"},
            {field: 'secondLevelName', width: 130, title: '二级科目', align: "center"},
            {field: 'secondLevelId', width: 130, hide: true, title: '二级科目id', align: "center"},
            {
                field: 'jine', title: '金额', width: 80, align: "center", event: 'setJine', style:'cursor: pointer;', templet: function (row) {
                    if (row.jine == null || row.jine == '') {
                        return "0";
                    } else {
                        return row.jine;
                    }
                }
            },
            {field: 'percentage', title: '占比', align: "center"},
            {field: 'content', title: '备注', align: "center",event: 'setContent', style:'cursor: pointer;'},
            {field: 'beiwang', title: '备忘', align: "center", event: 'setBeiwang', style:'cursor: pointer;'},
            {align: 'center', toolbar: '#tableBar', title: '', width: 100}
        ]],
        page: false
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/mdzx/index2'
    });

    // 导出excel
    $('#btnExp').click(function () {
        var btyZxMdzxId = Feng.getUrlParam("btyZxMdzxId");
        if (btyZxMdzxId == "" || btyZxMdzxId == undefined || btyZxMdzxId == null) {
            Feng.error("请先选择装修门店");
        } else {
            //获取导出Excel的数据
            var url = Feng.ctxPath + "/mdzx/listExport2?btyZxMdzxId=" + btyZxMdzxId;
            location.href = url;
        }
    });

    //监听单元格编辑-编辑备注和备忘时
    table.on('edit(mdzxTable)', function (obj) {
        //得到修改后的值
        var value = obj.value,
            //得到所在行所有键值
            data = obj.data,
            //得到字段
            field = obj.field;
        //金额字段校验
        if (field == "jine" && isNaN(value)) {
            Feng.error("修改失败，金额必须填写数字");
            return false;
        }
        //总计可编辑
        if (obj.data.firstLevelName == "总计") {
            Feng.error("修改失败，总计不可编辑");
            return false;
        }
        //总投资不可编辑
        if (obj.data.secondLevelName == "总投资") {
            Feng.error("修改失败，总投资不可编辑");
            return false;
        }
        //投资剩余不可编辑
        if (obj.data.secondLevelName == "投资剩余") {
            Feng.error("修改失败，投资剩余不可编辑");
            return false;
        }

        $.ajax({
            type: "PUT",
            url: "/zxTzqk/editTzqk",
            datatype: "json",
            data: data,
            success: function (data) {
                if (data.code == 200) {
                    if (field == "jine") {
                        table.reload(mdzxTable.tableId);
                    }
                }
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) {
            }
        });
    });

    //监听单元格事件
    table.on('tool(mdzxTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'setContent'&&data.firstLevelName==""&&data.secondLevelName!="总投资"&&data.secondLevelName!="投资剩余"){
            //编辑二级科目备注
            layer.prompt({
                formType: 2,
                title: contentTitle(data,'备注'),
                value: data.content
            }, function(value, index){
                layer.close(index);
                //这里一般是发送修改的Ajax请求,修改前后内容不变时才发送请求
                if(value!=data.content){
                    data.content=value;
                    $.ajax({
                        type: "PUT",
                        url: "/zxTzqk/editTzqk",
                        datatype: "json",
                        data: data,
                        success: function (data) {
                            if (data.code != 200) {
                                Feng.error(data.message);
                            }
                        }, error: function () {
                            Feng.error("修改失败！");
                        }, complete: function (XHR, TS) {
                        }
                    });
                    //同步更新表格和缓存对应的值
                    obj.update({
                        content: value
                    });
                }
            });
        }else if (obj.event === 'setBeiwang'&&data.firstLevelName==""&&data.secondLevelName!="总投资"&&data.secondLevelName!="投资剩余"){
            //编辑备忘
            layer.prompt({
                formType: 2,
                title: contentTitle(data,'备忘'),
                value: data.beiwang
            }, function(value, index){
                layer.close(index);
                //这里一般是发送修改的Ajax请求
                if(value!=data.content){
                    data.beiwang=value;
                    $.ajax({
                        type: "PUT",
                        url: "/zxTzqk/editTzqk",
                        datatype: "json",
                        data: data,
                        success: function (data) {
                            if (data.code != 200) {
                                Feng.error(data.message);
                            }
                        }, error: function () {
                            Feng.error("修改失败！");
                        }, complete: function (XHR, TS) {
                        }
                    });
                    //同步更新表格和缓存对应的值
                    obj.update({
                        beiwang: value
                    });
                }
            });
        }else if(obj.event === 'obtain'){
            mdzxTable.obtainFromBaoxiaodan(obj);
        }else if(obj.event === 'setJine'&&data.firstLevelName==""&&data.secondLevelName!="总投资"&&data.secondLevelName!="投资剩余"){
            mdzxTable.setJine(obj);
        }else if(obj.event === 'tzqkMingXi'){
            mdzxTable.tzqkMingXi(obj);
        }
    });
    /**
     * 金额计算
     */
    mdzxTable.tzqkMingXi = function (obj) {
        var data = obj.data;

        parent.layer.open({
            type: 2,
            title: mingXiTitle(data),
            area: ['800px', '600px'],
            content: Feng.ctxPath + '/mdzx/tzqkMingXi?btyZxMdzxId='+data.btyZxMdzxId+'&btyZxKemuId='+data.secondLevelId,
            end: function () {
                //table.reload(mdzxTable.tableId);
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    };

    function mingXiTitle(data) {
        return '['+ data.secondLevelName +']'+'报销明细:';
    }

    function contentTitle(data,colName) {
        if(data.firstLevelName!=""&&data.secondLevelName==""){
            return '修改 ['+ data.firstLevelName +'] 的'+colName
        }
        if(data.firstLevelName==""&&data.secondLevelName!=""){
            return '修改 ['+ data.secondLevelName +']的'+colName
        }
    }

    /**
     * 获取-重新获取该二级科目小的报销单数据，覆盖现存的
     */
    mdzxTable.obtainFromBaoxiaodan = function (obj) {
        var data = obj.data;

        var ajax = new $ax(Feng.ctxPath + "/zxTzqk/obtainFromBaoxiaodan", function (data) {
            if (data.code == 200) {
                Feng.success("获取成功!");
                table.reload(mdzxTable.tableId);
            } else {
                Feng.error(data.message);
            }
        });
        ajax.set("firstLevelId",data.firstLevelId);
        ajax.set("secondLevelId",data.secondLevelId);
        ajax.set("btyZxMdzxId", Feng.getUrlParam("btyZxMdzxId"));
        ajax.start();
    };

    function jineTitle(data) {
        if(data.firstLevelName!=""&&data.secondLevelName==""){
            return '['+ data.firstLevelName +']金额';
        }
        if(data.firstLevelName==""&&data.secondLevelName!=""){
            return '['+ data.secondLevelName +']金额';
        }
    }
    /**
     * 金额计算
     */
    mdzxTable.setJine = function (obj) {
        var data = obj.data;

        parent.layer.open({
            type: 2,
            height:600,
            title: jineTitle(data),
            area: ['400px', '600px'],
            content: Feng.ctxPath + '/mdzx/jinejisuan?jine=' + data.jine+"&btyZxMdzxId="+data.btyZxMdzxId+"&firstLevelId="+data.firstLevelId+"&secondLevelId="+data.secondLevelId,
            end: function () {
                table.reload(mdzxTable.tableId);
            },
            cancel:function (index){
                // Mdzx.search();
            }
        });
    };

    // 一键获取
    $('#obtainAll').click(function () {
        var ajax = new $ax(Feng.ctxPath + "/zxTzqk/obtainAllFromBaoxiaodan", function (data) {
            if (data.code == 200) {
                Feng.success("一键获取成功!");
                table.reload(mdzxTable.tableId);
            } else {
                Feng.error(data.message);
            }
        });
        ajax.set("btyZxMdzxId", Feng.getUrlParam("btyZxMdzxId"));
        ajax.start();
    });


});
